package com.huahan.hhbaseutils;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.huahan.hhbaseutils.imp.HHDialogListener;
import com.huahan.hhbaseutils.model.HHDialogParams;

/**
 * 对话框操作的工具类
 * @author yuan
 *
 */
public class HHDialogUtils 
{
	/**
	 * 提示builder
	 * @author xiao
	 *
	 */
	public static class Builder
	{
		private final Context mContext;
		private final HHDialogParams param;
		/**
		 * 创建builder对象
		 * @param context
		 */
		public Builder(Context context)
		{
			this.mContext=context;
			param=new HHDialogParams();
		}
		/**
		 * 创建一个新的实例
		 * @param context
		 * @return
		 */
		public static Builder getNewInstance(Context context) {
			return new Builder(context);
		}
		public Builder isShowTitle(boolean isShowTitle)
		{
			param.setShowTitle(isShowTitle);
			return this;
		}
		public Builder isShowAllBotton(boolean isShowAllBotton)
		{
			param.setShowAll(isShowAllBotton);
			return this;
		}
		public Builder setTitle(String title)
		{
			param.setTitle(title);
			return this;
		}
		public Builder setMessage(String message)
		{
			param.setMessage(message);
			return this;
		}
		public Builder setLeftBottonText(String leftBottonText)
		{
			param.setLeftMsg(leftBottonText);
			return this;
		}
		public Builder setLeftBottonTextColor(int leftBottonTextColor)
		{
			param.setLeftColor(leftBottonTextColor);
			return this;
		}
		public Builder setRightBottonText(String rightBottonText)
		{
			param.setRightMsg(rightBottonText);
			return this;
		}
		public Builder setRightBottonTextColor(int rightBottonTextColor)
		{
			param.setRightColor(rightBottonTextColor);
			return this;
		}
		public Builder setNegativeListener(HHDialogListener negativeListener)
		{
			param.setNegativeListener(negativeListener);
			return this;
		}
		public Builder setPositiveListener(HHDialogListener positiveListener)
		{
			param.setPositiveListener(positiveListener);
			return this;
		}
		public Dialog createOptionDialog()
		{
			final Dialog dialog = new Dialog(mContext, R.style.hh_dialog);
			View view = View.inflate(mContext, R.layout.hh_dialog_base_tip, null);
			TextView titleTextView = HHViewHelper.getViewByID(view,
					R.id.hh_tv_dialog_title);
			TextView msgTextView = HHViewHelper.getViewByID(view,
					R.id.hh_tv_dialog_msg);
			final TextView cancelTextView = HHViewHelper.getViewByID(view,
					R.id.hh_tv_dialog_cancel);
			final TextView sureTextView = HHViewHelper.getViewByID(view,
					R.id.hh_tv_dialog_sure);
			View lineView = HHViewHelper.getViewByID(view, R.id.hh_view);
			dialog.setContentView(view);
			android.view.WindowManager.LayoutParams attributes = dialog.getWindow()
					.getAttributes();
			attributes.width = HHScreenUtils.getScreenWidth(mContext)
					- HHDensityUtils.dip2px(mContext, 30);
			dialog.getWindow().setAttributes(attributes);
			/**
			/*是否显示title，默认显示*/
			titleTextView.setVisibility(param.isShowTitle()?View.VISIBLE:View.GONE);
			if (!TextUtils.isEmpty(param.getTitle())) {
				titleTextView.setText(param.getTitle());
			}
			if (!TextUtils.isEmpty(param.getMessage())) {
				msgTextView.setText(param.getMessage());
			}
			if (!TextUtils.isEmpty(param.getRightMsg())) {
				sureTextView.setText(param.getRightMsg());
			}
			if (param.getRightColor() > 0) {
				sureTextView.setTextColor(ContextCompat.getColorStateList(mContext, param.getRightColor()));
			}
			if (!TextUtils.isEmpty(param.getLeftMsg())) {
				cancelTextView.setText(param.getLeftMsg());
			}
			if (param.getLeftColor() > 0) {
				cancelTextView.setTextColor(ContextCompat.getColorStateList(mContext, param.getLeftColor()));
			}
			/*是否显示所有按钮，默认全部显示*/
			if (!param.isShowAll()) {
				lineView.setVisibility(View.GONE);
				cancelTextView.setVisibility(View.GONE);
				sureTextView.setBackgroundResource(R.drawable.hh_selector_dialog_click);
			}
			if (param.getNegativeListener()!=null) {
				cancelTextView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						param.getNegativeListener().onClick(dialog, cancelTextView);
					}
				});
			}
			if (param.getPositiveListener()!=null) {
				sureTextView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						param.getPositiveListener().onClick(dialog, sureTextView);
					}
				});
			}
			return dialog;
		}
	}
}
